package com.yjx.gulimall.product.service.impl;

import com.yjx.gulimall.common.vo.product.Images;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.product.dao.SkuImagesDao;
import com.yjx.gulimall.product.entity.SkuImagesEntity;
import com.yjx.gulimall.product.service.SkuImagesService;
import org.springframework.util.CollectionUtils;


@Service("skuImagesService")
public class SkuImagesServiceImpl extends ServiceImpl<SkuImagesDao, SkuImagesEntity> implements SkuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuImagesEntity> page = this.page(
                new Query<SkuImagesEntity>().getPage(params),
                new QueryWrapper<SkuImagesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean saveBatchInfo(Long skuId, List<Images> skuImages) {

        boolean flag = false;

        if (!CollectionUtils.isEmpty(skuImages)) {

            List<SkuImagesEntity> skuImagesEntities = skuImages.stream()
                    .filter(images -> StringUtils.isNotEmpty(images.getImgUrl()))
                    .map(images -> {
                        SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                        skuImagesEntity.setSkuId(skuId);
                        skuImagesEntity.setDefaultImg(images.getDefaultImg());
                        skuImagesEntity.setImgUrl(images.getImgUrl());
                        return skuImagesEntity;
                    })
                    .collect(Collectors.toList());
             flag = this.saveBatch(skuImagesEntities);
        }


        return flag;
    }

}